package com.sec.android.app.myfiles.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;

/**
 * Created by deukmolee on 16. 6. 29.
 */
public class ConfirmUnsupportedFileDialog extends AbsDialogFragment {

    private String mExtension;
    private String mDownloadFrom;
    private int mType;

    public static ConfirmUnsupportedFileDialog getInstance(String extension, int type, String downloadFrom) {
        ConfirmUnsupportedFileDialog dialog = new ConfirmUnsupportedFileDialog();
        dialog.mExtension = extension;
        dialog.mDownloadFrom = downloadFrom;
        dialog.mType = type;
        return dialog;
    }

    @Override
    protected Dialog _createDialog() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        if (mType == AppConstants.MarketType.PLAY_STORE) {
            builder.setTitle(R.string.unable_to_open_file);
            builder.setMessage(String.format(mContext.getString(
                    R.string.alert_popup_body_connect_to_play_store)));
        } else {
            builder.setTitle(getString(R.string.search_in_galaxy_apps, mDownloadFrom));
            builder.setMessage(String.format(mContext.getString(
                    R.string.alert_popup_body_connect_to_galaxy_apps), mDownloadFrom));
        }
        builder.setCancelable(false);
        builder.setPositiveButton(R.string.menu_search, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                try {
                    final Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    if (mType == AppConstants.MarketType.PLAY_STORE) {
                        intent.setData(Uri.parse("market://search?q=" + mExtension));
                    } else {
                        intent.setData(Uri.parse("samsungapps://SearchResult/"));
                        intent.putExtra("sKeyword", mExtension);
                    }
                    mContext.startActivity(intent);
                } catch (android.content.ActivityNotFoundException e) {
                    Toast.makeText(mContext, R.string.unable_to_find_application, Toast.LENGTH_LONG).show();
                }
                SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.FILE_NOT_OPEN_POPUP, SamsungAnalyticsLog.Event.SEARCH, null);
            }
        });

        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.FILE_NOT_OPEN_POPUP, SamsungAnalyticsLog.Event.CANCEL, null);
            }
        });

        return builder.create();
    }

    /**
     * Shows dialog using commitAllowingStateLoss for committing
     * FragmentTransaction. The only danger is that state of the parent Fragment
     * might omit the fact, that the dialog has been shown.
     */
    public void showAllowingStateLoss(final FragmentManager fm, final String tag) {
        if (fm != null) {
            final FragmentTransaction t = fm.beginTransaction();
            t.add(this, tag);
            t.commitAllowingStateLoss();
        }
    }
}
